package org.budo.spring.mvc.view.jphp;

import java.util.Locale;

import org.budo.spring.mvc.view.jphp.loader.DefaultJphpTemplateLoader;
import org.budo.spring.mvc.view.jphp.loader.JphpTemplateLoader;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;

/**
 * @author li
 * @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver
 */
public class JphpViewResolver extends AbstractTemplateViewResolver {
    private static final Logger log = Slf4j.getLogger();

    private JphpTemplateLoader templateLoader = new DefaultJphpTemplateLoader();

    public JphpViewResolver() {
        this.setViewClass(JphpView.class);
    }

    @Override
    protected Class<?> requiredViewClass() {
        return JphpView.class;
    }

    public View resolveViewName(String viewName, Locale locale) throws Exception {
        JphpView view = (JphpView) super.resolveViewName(viewName, locale);
        view.setTemplateLoader(this.getTemplateLoader());

        log.debug("resolveViewName, viewName=" + viewName + ", locale=" + locale + ", view=" + view);
        return view;
    }

    public JphpTemplateLoader getTemplateLoader() {
        return templateLoader;
    }

    public void setTemplateLoader(JphpTemplateLoader templateLoader) {
        this.templateLoader = templateLoader;
    }
}